package com.liuxingzhiwu.ruijishangcheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.liuxingzhiwu.ruijishangcheng.common.R;
import com.liuxingzhiwu.ruijishangcheng.entity.ShoppingCart;
import com.liuxingzhiwu.ruijishangcheng.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     * @param request
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String> addShoppingCart(HttpServletRequest request, @RequestBody ShoppingCart shoppingCart){
        //添加用户id，此处为默认
        shoppingCart.setUserId(001L);

        //在购物车里面添加菜品
        //判断购物车里是否有此菜品
        if(shoppingCart.getDishId()!=null) {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            //菜品的id和口味相同，菜品数量加1，否则为新菜品，添加
            queryWrapper.eq("dish_id", shoppingCart.getDishId());
            queryWrapper.eq("dish_flavor",shoppingCart.getDishFlavor());
            queryWrapper.select("number");
            ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
            if (shoppingCart1 == null) {
                //菜品不同
                shoppingCartService.save(shoppingCart);
            } else {
                //菜品相同
                int number = shoppingCart1.getNumber();
                UpdateWrapper<ShoppingCart> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("dish_id", shoppingCart.getDishId());
                updateWrapper.set("number", number + 1);
                shoppingCartService.update(updateWrapper);
            }
        }

        //在购物车里面添加套餐
        if(shoppingCart.getSetmealId()!=null){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("setmeal_id", shoppingCart.getSetmealId());
            queryWrapper.select("number");
            ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
            if (shoppingCart1 == null) {
                //套餐不同
                shoppingCartService.save(shoppingCart);
            } else {
                //套餐相同
                Integer number = shoppingCart1.getNumber();
                UpdateWrapper<ShoppingCart> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("setmeal_id", shoppingCart.getSetmealId());
                updateWrapper.set("number", number + 1);
                shoppingCartService.update(updateWrapper);
            }
        }
        return R.success("添加成功");
    }

    /**
     * 获取购物车
     * @return
     */
    @GetMapping("/list")
    public R<List> getShoppingCart(){
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

}
